package com.lzq.springblog;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.UUID;
@SpringBootTest
public class SecurityUtils {

    @Test
    public void encrypt() {
        String password = "123456";
        String salt = UUID.randomUUID().toString().replace("-","");
        String finalPassword = DigestUtils.md5DigestAsHex((salt+password).getBytes());
        System.out.println((salt + finalPassword));
    }

    public static Boolean verify(String password,String finalPassword) {
        if(!StringUtils.hasLength(password) || !StringUtils.hasLength(finalPassword)) {
            return false;
        }
        if(finalPassword.length()!=64) {
            return false;
        }
        String salt = finalPassword.substring(0,32);
        String finalPassword2 = DigestUtils.md5DigestAsHex((salt+password).getBytes());
        return (salt+finalPassword2).equals(finalPassword);
    }

}
